package com.opennews.openplatform

import com.opennews.openplatform.constants.IncomeExpenseTypeConstant
import groovy.transform.CompileStatic
import org.springframework.http.HttpStatus

@CompileStatic
class IncomeExpenseDetailTypeController extends BaseController {
    static allowedMethods = [
        resetOrder: "POST",
        add: "POST",
        update: "POST",
        delete: "POST",
        batchDelete: "POST"
    ]

    IncomeExpenseDetailTypeService incomeExpenseDetailTypeService

    // region: Get actions.
    def index() {
        render("IncomeExpenseDetailTypeController")
    }

    def queryByAccountGroup() {
        def incomeExpenseType = params.incomeExpenseType.toString()
        def accountGroupId = getClaims().accountGroupId
        def result = incomeExpenseDetailTypeService.queryByAccountGroup(accountGroupId, incomeExpenseType)

        sendDataResponse(result)
    }

    def checkExisting() {
        def incomeExpenseType = params.incomeExpenseType.toString()
        def id = params.id.toString()
        def name = params.name.toString()
        def accountGroupId = getClaims().accountGroupId
        def result = incomeExpenseDetailTypeService.checkExisting(accountGroupId, incomeExpenseType, id, name)

        sendDataResponse(result)
    }

    def queryAnalysisNotIncludedDetailTypes() {
        def accountGroupId = getClaims().accountGroupId
        def incomeDetailTypes = incomeExpenseDetailTypeService.queryAnalysisNotIncludedDetailTypes(accountGroupId, IncomeExpenseTypeConstant.INCOME)
        def expenseDetailTypes = incomeExpenseDetailTypeService.queryAnalysisNotIncludedDetailTypes(accountGroupId, IncomeExpenseTypeConstant.EXPENSE)

        sendDataResponse([
            incomeDetailTypes: incomeDetailTypes,
            expenseDetailTypes: expenseDetailTypes
        ])
    }
    // endregion

    // region: Post actions.
    def resetOrder() {
        def requestBodyData = getRequestBodyData()
        def detailTypeIds = requestBodyData.detailTypeIds as List<String>
        def accountGroupId = getClaims().accountGroupId
        incomeExpenseDetailTypeService.resetOrder(accountGroupId, detailTypeIds)

        sendStatusResponse(HttpStatus.NO_CONTENT)
    }

    def add() {
        def requestBodyData = getRequestBodyData()
        def accountGroupId = getClaims().accountGroupId
        def result = incomeExpenseDetailTypeService.add(requestBodyData, accountGroupId)

        sendDataResponse(result)
    }

    def update() {
        def requestBodyData = getRequestBodyData()
        def accountGroupId = getClaims().accountGroupId
        def result = incomeExpenseDetailTypeService.update(requestBodyData, accountGroupId)

        sendDataResponse(result)
    }

    def delete() {
        def requestBodyData = getRequestBodyData()
        def incomeExpenseType = requestBodyData.incomeExpenseType.toString()
        def name = requestBodyData.name.toString()
        def accountGroupId = getClaims().accountGroupId
        def result = incomeExpenseDetailTypeService.delete(incomeExpenseType, name, accountGroupId)

        sendDataResponse(result)
    }

    def batchDelete() {
        def requestBodyData = getRequestBodyData()
        def incomeExpenseType = requestBodyData.incomeExpenseType.toString()
        def names = requestBodyData.names as List
        def accountGroupId = getClaims().accountGroupId
        def result = incomeExpenseDetailTypeService.delete(incomeExpenseType, names, accountGroupId)

        sendDataResponse(result)
    }
    // endregion
}